package com.atguigu.service.impl;

import com.atguigu.dao.PortDao;
import com.atguigu.dao.ServiceDao;
import com.atguigu.dao.impl.PortDaoImpl;
import com.atguigu.dao.impl.ServiceDaoImpl;
import com.atguigu.pojo.*;
import com.atguigu.pojo.Service;
import com.atguigu.service.ServerService;
import com.atguigu.service.ServiceService;
import com.atguigu.utils.WebUtils;

import java.util.ArrayList;
import java.util.List;
//这是面向程序的接口,不关心sql,关心对象
public class ServiceServiceImpl implements ServiceService {
    ServerService serverService=new ServerServiceImpl();
    ServiceDao serviceDao=new ServiceDaoImpl();//这里面是sql,接受参数,组织sql,执行mysql查询;所以它是公共的的类,增删改查就用它的方法实现
    PortDao portDao=new PortDaoImpl();
    @Override
    public void addService(Service service) {
        serviceDao.addService(service);
//        List<Port> savedPorts=portDao.queryPortsByServiceId(service.getId());
//        for (Port port:service.getPorts()) {
//            portDao.addPort(port);
//        }//添加服务不需要添加端口,添加配置才需要添加端口
    }

    @Override
    public void updateService(Service service) {
        Service oldService=queryServiceById(service.getId());
        String oldServicePorts=oldService.getPorts();//这就是老的service端口集
        //从server中删除老的service集,在加入新的service port集
        Server server=serverService.queryServerById(service.getServerId());
        String oldServerPorts=server.getPorts();
        //oldServerPorts,oldServicePorts
        String tmpServerPorts=WebUtils.reducePortItem(oldServerPorts,oldServicePorts);
        String newServerPorts=WebUtils.addPortsItem(tmpServerPorts,service.getPorts());
        String newFreePorts=WebUtils.getFreePorts(newServerPorts);
        //System.out.println("newServerPorts"+newServerPorts);
        //System.out.println("newFreePorts"+newFreePorts);
        server.setFreePorts(newFreePorts);
        server.setPorts(newServerPorts);
        serverService.updateServer(server);

        serviceDao.updateService(service);
    }

    @Override
    public void deleteServiceById(String id) {
        serviceDao.deleteServiceById(id);
    }

    @Override
    public Service queryServiceById(String id) {
        Service service=serviceDao.queryServiceById(id);
        return service;
    }
    @Override
    public List<Service> queryServicesByServerId(String serverId) {
        List<Service> services=serviceDao.queryServicesByServerId(serverId);
        return services;
    }
    @Override
    public List<Service> queryServicesByServerIdAndServiceName(String serverId,String serviceName) {
        List<Service> services=serviceDao.queryServicesByServerIdAndServiceName(serverId,serviceName);
        return services;
    }
    @Override
    public List<Service> queryServices() {
        List<Service> queryServices=serviceDao.queryServices();
        return queryServices;
    }

    @Override
    public List<Service> queryServicesByName(String serviceName){
        List<Service> services=serviceDao.queryServicesByName(serviceName);
        return services;
    }
    @Override
    public List<Server> queryServersByServiceName(String serviceName){
        List<Service> services=serviceDao.queryServicesByName(serviceName);
        List<Server> servers=new ArrayList();
        for (Service service:services) {
            Server server=serverService.queryServerById(service.getServerId());
            servers.add(server);
        }
        return servers;
    }
    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<Service> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=serviceDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<Service> items=serviceDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
